<?php
class ChallengeOrderService {
    /**
     * @var ChallengeOrderService
     */
    private static $instance;

    /**
     * Get the instance of ChallengeOrderService.
     *
     * @return ChallengeOrderService
     */
    public static function get() {
        if (!self::$instance) {
            self::$instance = new ChallengeOrderService();
        }
        return self::$instance;
    }

    public function __construct() {

    }

    /**
     * 触发订单挑战
     * @param $userId
     * @param $triggerTimes
     * @param $playTimes
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function triggerChallengeOrder($userId,$triggerTimes,$playTimes,$reqTime){
        $status = 1;

        $isTrigger = $this->checkTrigger($userId,$triggerTimes,$reqTime);
        if ($isTrigger !=0){
           return  array(
               'code' => $status
           );
        }

        $Ymd = date('Ymd',$reqTime);
        /**
         * @var ChallengeOrderVoList $challengeOrderList
         * @var ChallengeOrderVo $challengeOrder
         */
        $challengeOrderList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CHALLENGE_ORDER);
        $challengeOrder = $challengeOrderList->getElement($triggerTimes.'_'.$playTimes,false);
        if (!$challengeOrder) {
            $status = 0;
            $challengeOrder = new ChallengeOrderVo($userId,$triggerTimes.'_'.$playTimes,$Ymd,0,1,0,0,0,[],0,0,[],0,0,$reqTime,0,true);
            $challengeOrderList->addElement($challengeOrder);
            $challengeOrderList->setElement($triggerTimes.'_'.$playTimes,$challengeOrder);
        } else if (date('Ymd',$challengeOrder->getCreateTime()) != $Ymd) {
            $status = 0;
            $challengeOrder->setYmd($Ymd);
            $challengeOrder->setStatus(0);
            $challengeOrder->setIsFree(1);
            $challengeOrder->setCompleteNum(0);
            $challengeOrder->setExp(0);
            $challengeOrder->setGold(0);
            $challengeOrder->setCompleteOrder([]);
            $challengeOrder->setStartTime(0);
            $challengeOrder->setEndTime(0);
            $challengeOrder->setReward([]);
            $challengeOrder->setRewardStatus(0);
            $challengeOrder->setRewardTime(0);
            $challengeOrder->setCreateTime($reqTime);
            $challengeOrder->setUpdateTime(0);
            $challengeOrderList->updateElement($triggerTimes.'_'.$playTimes,$challengeOrder);
        } else if ($challengeOrder->getStatus() == GameConst::CHALLENGE_ORDER_STATUS_TRIGGER) {
            $status = 0;
        }

        SzPersister::get()->setVoList($challengeOrderList);
        return array(
            'code' => $status
        );
    }

    /**
     * 拒绝挑战
     * @param $userId
     * @param $triggerTimes
     * @param $playTimes
     * @param $reqTime
     * @return array
     * @throws SzException
     */
     public function refuseChallengeOrder($userId,$triggerTimes,$playTimes,$reqTime){
         $status = 1;
         $isTrigger = $this->checkTrigger($userId,$triggerTimes,$reqTime);
         if ($isTrigger !=0){
             return  array(
                 'code' => $status
             );
         }

         $Ymd = date('Ymd',$reqTime);
         /**
          * @var ChallengeOrderVoList $challengeOrderList
          * @var ChallengeOrderVo $challengeOrder
          */
         $challengeOrderList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CHALLENGE_ORDER);
         $challengeOrder = $challengeOrderList->getElement($triggerTimes.'_'.$playTimes,false);
         if (date('Ymd',$challengeOrder->getCreateTime()) != $Ymd || !$challengeOrder){
             return  $status;
         }
         $status = 0;
         $challengeOrder->setStatus(2);
         $challengeOrder->setUpdateTime($reqTime);
         $challengeOrderList->updateElement($triggerTimes.'_'.$playTimes,$challengeOrder);

         SzPersister::get()->setVoList($challengeOrderList);
         return array(
             'code' => $status
         );
     }

    /**
     * 接受挑战
     * @param $userId
     * @param $triggerTimes
     * @param $playTimes
     * @param $reqTime
     * return array
     */
     public function acceptChallengeOrder($userId,$triggerTimes,$playTimes,$reqTime){
         $isTrigger = $this->checkTrigger($userId,$triggerTimes,$reqTime);
         if ($isTrigger !=0){
             return array(
                 'code' => 999, //挑战条件不满足
             );
         }

         $Ymd = date('Ymd',$reqTime);
         /**
          * @var ChallengeOrderVoList $challengeOrderList
          * @var ChallengeOrderVo $challengeOrder
          */
         $challengeOrderList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CHALLENGE_ORDER);
         $challengeOrder = $challengeOrderList->getElement($triggerTimes.'_'.$playTimes,false);
         if (date('Ymd',$challengeOrder->getCreateTime()) != $Ymd || !$challengeOrder){
             return array(
                 'code' => 998, //还没有触发挑战
             );
         }

         $data = $this->getFlowerOrderInfo($userId,$triggerTimes,$playTimes,$reqTime);
         $targets = $data['targets'];
         $reward = $data['reward'];
         $rewardMultiple = $data['rewardMultiple'];
         $completeNum = 0;

         $challengeOrderConf = $this->getChallengeConf();

         $challengeMulti = GameConst::CHALLENGE_ORDER_MULTI;
         $endTime = $reqTime + $challengeOrderConf['durationTime'] + $challengeMulti;//挑战结束时间

         $challengeOrder->setStatus(1);
         $challengeOrder->setStartTime($reqTime);
         $challengeOrder->setEndTime($endTime);
         $challengeOrder->setUpdateTime($reqTime);
         $challengeOrderList->updateElement($triggerTimes.'_'.$playTimes,$challengeOrder);

         SzPersister::get()->setVoList($challengeOrderList);

         return array(
             'code' => 0,
             'targets' => $targets,
             'reward' => $reward,
             'rewardMultiple' => $rewardMultiple,
             'completeNum' => $completeNum
         );
     }

    /**
     * @param $userId
     * @param $triggerTimes
     * @param $playTimes
     * @param $reqTime
     * return array
     */
     public function refreshChallengeOrder($userId,$triggerTimes,$playTimes,$reqTime){
         $isTrigger = $this->checkTrigger($userId,$triggerTimes,$reqTime);
         if ($isTrigger !=0){
             return array(
                 'code' => 999, //挑战条件不满足
             );
         }

         $Ymd = date('Ymd',$reqTime);
         /**
          * @var ChallengeOrderVoList $challengeOrderList
          * @var ChallengeOrderVo $challengeOrder
          */
         $challengeOrderList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CHALLENGE_ORDER);
         $challengeOrder = $challengeOrderList->getElement($triggerTimes.'_'.$playTimes,false);
         if (date('Ymd',$challengeOrder->getCreateTime()) != $Ymd || !$challengeOrder){
             return array(
                 'code' => 998, //还没有触发挑战
             );
         }

         $challengeOrderConf = $this->getChallengeConf();
         if ($challengeOrder->getCompleteNum() > $challengeOrderConf['orderMax']){
             return array(
                 'code' => 997, //订单完成次数达上限
             );
         }

         $data = $this->getFlowerOrderInfo($userId,$triggerTimes,$playTimes,$reqTime);
         $targets = $data['targets'];
         $rewardMultiple = $data['rewardMultiple'];
         $completeNum = $challengeOrder->getCompleteNum();
         if ($completeNum >= 1){
            $gold = $challengeOrder->getGold();
            $exp = $challengeOrder->getExp();
            $reward = array(
             GameConst::ENTITY_ACTION_GET_EXP => intval($exp),
             GameConst::ENTITY_ACTION_GET_TOKEN => intval($gold)
            );
         }else{
             //$reward = $data['reward'];
             $reward = array(
                 GameConst::ENTITY_ACTION_GET_EXP => 0,
                 GameConst::ENTITY_ACTION_GET_TOKEN => 0
             );
         }

         return array(
             'code' => 0,
             'targets' => $targets,
             'reward' => $reward,
             'rewardMultiple' => $rewardMultiple,
             'completeNum' => $completeNum,
         );
     }

    /**
     * 再玩一次
     * @param $userId
     * @param $triggerTimes
     * @param $playTimes
     * @param $reqTime
     * @return array
     * @throws SzException
     */
     public function againChallengeOrder($userId,$triggerTimes,$playTimes,$reqTime){
         $isTrigger = $this->checkTrigger($userId,$triggerTimes,$reqTime);
         if ($isTrigger !=0){
             return array(
                 'code' => 999, //挑战条件不满足
             );
         }

         $Ymd = date('Ymd',$reqTime);
         /**
          * @var ChallengeOrderVoList $challengeOrderList
          * @var ChallengeOrderVo $challengeOrder
          */
         $challengeOrderList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CHALLENGE_ORDER);
         $challengeOrder = $challengeOrderList->getElement($triggerTimes.'_'.$playTimes,false);

         if ($challengeOrder && date('Ymd',$challengeOrder->getCreateTime()) == $Ymd){
             return array(
                 'code' => 996, //再玩一次只能玩一次
             );
         }

         $challengeOrderConf = $this->getChallengeConf();

         $challengeMulti = GameConst::CHALLENGE_ORDER_MULTI;
         $endTime = $reqTime + $challengeOrderConf['durationTime'] + $challengeMulti;//挑战结束时间

         if (!$challengeOrder) {
             $challengeOrder = new ChallengeOrderVo($userId,$triggerTimes.'_'.$playTimes,$Ymd,1,2,0,0,0,[],$reqTime,$endTime,[],0,0,$reqTime,0,true);
             $challengeOrderList->addElement($challengeOrder);
             $challengeOrderList->setElement($triggerTimes.'_'.$playTimes,$challengeOrder);
         } else if (date('Ymd',$challengeOrder->getCreateTime()) != $Ymd) {
             $challengeOrder->setYmd($Ymd);
             $challengeOrder->setCompleteNum(0);
             $challengeOrder->setExp(0);
             $challengeOrder->setGold(0);
             $challengeOrder->setCompleteOrder([]);
             $challengeOrder->setStartTime($reqTime);
             $challengeOrder->setEndTime($endTime);
             $challengeOrder->setReward([]);
             $challengeOrder->setRewardStatus(0);
             $challengeOrder->setRewardTime(0);
             $challengeOrder->setCreateTime($reqTime);
             $challengeOrder->setUpdateTime(0);
             $challengeOrderList->updateElement($triggerTimes.'_'.$playTimes,$challengeOrder);
         }

         SzPersister::get()->setVoList($challengeOrderList);

         //再玩一次的消耗
         $input = '';
         foreach ($challengeOrderConf['onceMore'] as $entityId => $count) {
             GameService::get()->handleEntity($userId, $entityId, $count, true, true);
             $input .= $entityId . ':' . abs($count) . '|';
         }
         CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_CHALLENGE_ORDER,$userId,0,$input,'againChallengeOrder');//消耗资源上报

         $data = $this->getFlowerOrderInfo($userId,$triggerTimes,$playTimes,$reqTime);
         $targets = $data['targets'];
         //$reward = $data['reward'];
         $reward = array(
             GameConst::ENTITY_ACTION_GET_EXP => 0,
             GameConst::ENTITY_ACTION_GET_TOKEN => 0
         );
         $rewardMultiple = $data['rewardMultiple'];
         $completeNum = 0;

         return array(
             'code' => 0,
             'targets' => $targets,
             'reward' => $reward,
             'rewardMultiple' => $rewardMultiple,
             'completeNum' => $completeNum
         );

     }

    /**
     * 完成订单
     * @param $userId
     * @param $triggerTimes
     * @param $playTimes
     * @param $flowerId
     * @param $flowerNum
     * @param $reqTime
     * @return array
     * @throws SzException
     */
     public function completeChallengeOrder($userId,$triggerTimes,$playTimes,$flowerId,$flowerNum,$reqTime){
         $isTrigger = $this->checkTrigger($userId,$triggerTimes,$reqTime);
         if ($isTrigger !=0){
             return array(
                 'code' => 999, //挑战条件不满足
             );
         }

         $Ymd = date('Ymd',$reqTime);
         /**
          * @var ChallengeOrderVoList $challengeOrderList
          * @var ChallengeOrderVo $challengeOrder
          */
         $challengeOrderList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CHALLENGE_ORDER);
         $challengeOrder = $challengeOrderList->getElement($triggerTimes.'_'.$playTimes,false);
         if (date('Ymd',$challengeOrder->getCreateTime()) != $Ymd || !$challengeOrder){
             return array(
                 'code' => 998, //还没有触发挑战
             );
         }

         if ($reqTime > $challengeOrder->getEndTime()) {
             return array(
                 'code' => 995, //本次挑战的时间已结束
             );
         }

         $challengeOrderConf = $this->getChallengeConf();
         $completeNum = $challengeOrder->getCompleteNum();
         if ($completeNum > $challengeOrderConf['orderMax']){
             return array(
                 'code' => 997, //订单完成次数达上限
             );
         }

         //验证鲜花订单
         $orderInfo = SzPersister::get()->getVo($userId, GameConst::ORM_NAME_CHALLENGE_ORDER_INFO);
         if (date('Ymd',$orderInfo->getCreateTime()) != $Ymd || !$orderInfo){
             return array(
                 'code' => 994, //订单不正确
             );
         }

         $target = $orderInfo->getRawTarget();
         //扣除鲜花
         $input =  '';
         foreach ($target as $entityId => $count) {
             if ($entityId != $flowerId || $count != $flowerNum){
                 return array(
                     'code' => 993, //订单鲜花数量或鲜花不正确
                 );
             }
             GameService::get()->handleEntity($userId, $entityId, $count, true, true);
             $input .= $entityId . ':' . abs($count) . '|';

         }
         CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_CHALLENGE_ORDER,$userId,0,$input,'completeChallengeOrder');//消耗资源上报

         $reward = $orderInfo->getRawReward();//鲜花订单的奖励

         $gold = $challengeOrder->getGold();
         $exp = $challengeOrder->getExp();

         foreach ($reward as $entityId => $count) {
             if ( $entityId == GameConst::ENTITY_ACTION_GET_TOKEN) {
                 $gold += $count;
             }elseif ($entityId == GameConst::ENTITY_ACTION_GET_EXP) {
                 $exp += $count;
             }
         }
         $challengeOrder->setCompleteNum(++$completeNum);
         $challengeOrder->setGold($gold);
         $challengeOrder->setExp($exp);
         $challengeOrder->setUpdateTime($reqTime);
         $challengeOrderList->updateElement($triggerTimes.'_'.$playTimes,$challengeOrder);

         SzPersister::get()->setVoList($challengeOrderList);

         $data = $this->getFlowerOrderInfo($userId,$triggerTimes,$playTimes,$reqTime);
         $targets = $data['targets'];
         $rewardMultiple = $data['rewardMultiple'];
         $completeNum = $challengeOrder->getCompleteNum();
         if ($completeNum >= 1){
             $gold = $challengeOrder->getGold();
             $exp = $challengeOrder->getExp();
             $reward = array(
                 GameConst::ENTITY_ACTION_GET_EXP => intval($exp),
                 GameConst::ENTITY_ACTION_GET_TOKEN => intval($gold)
             );
         }else{
             $reward = $data['reward'];
         }

         return array(
             'code' => 0,
             'targets' => $targets,
             'reward' => $reward,
             'rewardMultiple' => $rewardMultiple,
             'completeNum' => $completeNum
         );
     }

    /**
     * 获取挑战的奖励信息
     * @param $userId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
     public function challengeOrderRewardInfo($userId,$reqTime){
         $Ymd = date('Ymd',$reqTime);

         $challengeOrderList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CHALLENGE_ORDER);
         $challengeOrder = array();
         if ($challengeOrderList->getListCount() != 0) {
            $challengeInfo = $challengeOrderList->toArray();
            //获取未领取奖励的挑战
            foreach ($challengeInfo as $key => $value) {
                $createTime = $value['createTime'];
                $startTime = $value['startTime'];
                $rewardStatus = $value['rewardStatus'];
                $status = $value['status'];

                if (date('Ymd',$createTime) == $Ymd && $reqTime > $startTime && $rewardStatus == 0  && $status == GameConst::CHALLENGE_ORDER_STATUS_ACCEPT) {
                    $challengeOrder = $value;
                    break;
                }
            }
         }

         if (!$challengeOrder) {
             return array(
                'code' => 992, //没有待领取的挑战奖励
             );
         }
         $completeNum = $challengeOrder['completeNum'];
         $indexId = $challengeOrder['indexId'];
         $trigger_play =explode('_',$indexId);
         $triggerTimes = $trigger_play[0];
         $playTimes = $trigger_play[1];

         $challengeOrderConf = $this->getChallengeConf();
         $rewardMultiple = 1;
         if ($completeNum > $challengeOrderConf['guaranteedCondition']){
             foreach ($challengeOrderConf['multipleSection'] as $key=>$value) {
                 if ($completeNum <= $value) {
                     $multipleIndex = $key;
                     break;
                 }
             }
             $rewardMultiple = $challengeOrderConf['rewardMultiple'][$multipleIndex];
         }

         if ($completeNum <= $challengeOrderConf['guaranteedCondition']) {
             $reward = $challengeOrderConf['guaranteedRewards'];
         }else{
             $gold = $challengeOrder['gold'];
             $exp = $challengeOrder['exp'];
             $reward = array(
                 GameConst::ENTITY_ACTION_GET_EXP => intval($exp),
                 GameConst::ENTITY_ACTION_GET_TOKEN => intval($gold)
             );
         }

         return array(
             'code' => 0,
             'triggerTimes' => $triggerTimes,
             'playTimes' => $playTimes,
             'reward' => $reward,
             'rewardMultiple' => $rewardMultiple,
             'completeNum' => $completeNum
         );
     }

     public function challengeOrderReward($userId,$triggerTimes,$playTimes,$reqTime){
         $Ymd = date('Ymd',$reqTime);
         /**
          * @var ChallengeOrderVoList $challengeOrderList
          * @var ChallengeOrderVo $challengeOrder
          */
         $challengeOrderList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CHALLENGE_ORDER);
         $challengeOrder = $challengeOrderList->getElement($triggerTimes.'_'.$playTimes,false);
         if (date('Ymd',$challengeOrder->getCreateTime()) != $Ymd || !$challengeOrder){
             return array(
                 'code' => 998, //还没有触发挑战
             );
         }

         $challengeOrderConf = $this->getChallengeConf();
         $completeNum = $challengeOrder->getCompleteNum();

         //获取奖励的倍数
         foreach ($challengeOrderConf['multipleSection'] as $key=>$value) {
             if ($completeNum <= $value) {
                 $multipleIndex = $key;
                 break;
             }
         }
         $rewardMultiple = $challengeOrderConf['rewardMultiple'][$multipleIndex];

         if ($completeNum <= $challengeOrderConf['guaranteedCondition']) {
             $reward = $challengeOrderConf['guaranteedRewards'];
         }else{
             $gold = $challengeOrder->getGold();
             $exp = $challengeOrder->getExp();
             $reward = array(
                 GameConst::ENTITY_ACTION_GET_EXP => intval($exp*$rewardMultiple),
                 GameConst::ENTITY_ACTION_GET_TOKEN => intval($gold*$rewardMultiple)
             );
         }

         //获取奖励
         $output =  '';
         foreach ($reward as $entityId => $count) {
             GameService::get()->handleEntity($userId, $entityId, $count, true, true);
             $output .= $entityId . ':' . abs($count) . '|';

         }
         CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_CHALLENGE_ORDER,$userId,1,$output,'challengeOrderReward');//消耗资源上报


         $challengeOrder->setRewardStatus(1);
         $challengeOrder->setReward($reward);
         $challengeOrder->setRewardTime($reqTime);
         $challengeOrder->setUpdateTime($reqTime);
         $challengeOrderList->updateElement($triggerTimes.'_'.$playTimes,$challengeOrder);

         SzPersister::get()->setVoList($challengeOrderList);

         return array(
             'code' => 0
         );
     }

    /**
     * 获取订单挑战的配置
     * @return array
     */
    private function getChallengeConf(){
        $challengeOrderSettings = SzConfig::get()->loadAppConfig(GameConst::CONFIG_ORDER_CHALLENGE, null, true);
        $challengeOrderConf = array();
        foreach ($challengeOrderSettings as $challengeOrderSetting) {
            if($challengeOrderSetting){
                $challengeOrderConf = $challengeOrderSetting;
                break;
            }
        }
        return $challengeOrderConf;
    }
    /**
     * @param $userId
     * @param $triggerTimes
     * @param $reqTime
     * @return int
     */
    private function checkTrigger($userId,$triggerTimes,$reqTime){
        $result = 0;
        $challengeOrderConf = $this->getChallengeConf();

        if (!$challengeOrderConf){
            $result = 1; //缺少配置
        }

        $totalTriggerTimes =count($challengeOrderConf['challengingCondition']);
        if ($triggerTimes > $totalTriggerTimes){
            $result = 2; //触发挑战已达上线
        }

        $needFlowerOrderNum = $challengeOrderConf['challengingCondition'][$triggerTimes-1];
        //获取当天完成的鲜花订单
        $flowerOrderNum = $this->getFlowerOrderNum($userId,$reqTime);
        if ($flowerOrderNum < $needFlowerOrderNum){
            $result = 3; //完成鲜花订单不足
        }

        return $result;
    }

    /**
     * 获取完成鲜花订单的数量
     * @param $userId
     * @param $reqTime
     * @return int
     * @throws SzException
     */
    public function getFlowerOrderNum($userId,$reqTime){
        $Ymd=date('Ymd',$reqTime);
        $dateInt=strtotime($Ymd);
        /**
         * @var $userShopList UserShopVoList
         * @var $userShop  UserShopVo
         */

        $userShopList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_SHOP);
        $userShop = $userShopList->getElement($dateInt,false);
        if (!$userShop){
            $num = 0;//当天没有完成鲜花订单
        }else{
            $num = $userShop->getFlowerOrder();
        }

        return $num;

    }

    /**
     * @param $userId
     * @param $triggerTimes
     * @param $playTimes
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getFlowerOrderInfo($userId,$triggerTimes,$playTimes,$reqTime) {
        $challengeOrderConf = $this->getChallengeConf();

        /**
         * @var ChallengeOrderVoList $challengeOrderList
         * @var ChallengeOrderVo $challengeOrder
         */
        $challengeOrderList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CHALLENGE_ORDER);
        $challengeOrder = $challengeOrderList->getElement($triggerTimes.'_'.$playTimes,false);

        $completeNum = $challengeOrder->getCompleteNum();

        //获取挑战的鲜花数量
        $quantity = 0;
        foreach ($challengeOrderConf['flowerSection'] as $key=>$value) {
            if ($completeNum <= $value) {
                $numIndex = $key;
                break;
            }
        }
        $quantity = $challengeOrderConf['flowerNum'][$numIndex];

       //获取奖励的倍数
        $rewardMultiple = 1;
        foreach ($challengeOrderConf['multipleSection'] as $key=>$value) {
            if ($completeNum <= $value) {
                $multipleIndex = $key;
                break;
            }
        }
        $rewardMultiple = $challengeOrderConf['rewardMultiple'][$multipleIndex];

        //获取从哪个库获取鲜花
        $allRatio = 0;
        foreach ($challengeOrderConf['probabilitySection'] as $key=>$value) {
            if ($completeNum <= $value) {
                $flowerPoolIndex = $key;
                break;
            }
        }
        $allRatio = $challengeOrderConf['probability'][$flowerPoolIndex];
        $randomNum = rand(1, 100);
        if ($randomNum <= $allRatio){
            $allFlowersSettings = SzConfig::get()->loadAppConfig(GameConst::CONFIG_ORDER_REWARD, null, true);
            $allFlowers = array();
            foreach ($allFlowersSettings as $key => $flowersSetting){
                $allFlowers[$key] = $flowersSetting['id'];
            }
            $flowerId = intval(array_rand($allFlowers));
        }else{
            $unlockFlowers = SeedService::get()->getUnlockFlowerList($userId);
            $key_unlock = array_rand($unlockFlowers);
            $flowerId = $unlockFlowers[$key_unlock];
        }

        $buildEntityId = EntityService::get()->buildEntityId(GameConst::ENTITY_ACTION_CONSUME,GameConst::ENTITY_TYPE_ITEM,$flowerId);
        $targets = array(
            $buildEntityId => $quantity
        );
        $orderRewardSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_ORDER_REWARD, $flowerId, true);
        $reward = array();
//        $reward = array(
//            GameConst::ENTITY_ACTION_GET_EXP => intval($orderRewardSetting['experience']*$quantity),
//            GameConst::ENTITY_ACTION_GET_TOKEN => intval($orderRewardSetting['gold']*$quantity)
//        );

        $goldNum = 0;
        $experienceNum = 0;
        foreach ($targets as $fid=>$flowerNum){
            $flower = substr($fid,-8);
            //20200409
            //获取玩家该花朵是否具有金币数量加成
            $flowerReward = SzConfig::get()->loadAppConfig(GameConst::CONFIG_ORDER_REWARD, $flower, true);
            $goldNum = $flowerReward['gold']*$flowerNum;
            $experienceNum = $flowerReward['experience']*$flowerNum;
            $cashBuff = FlowersUpgradeService::get()->getFlowerBuff($userId,$flower,GameConst::FLOWER_TYPE_1);
            if (!empty($cashBuff)){
                if ($cashBuff[0] == 2){
                    $goldNum = ($flowerReward['gold'] + $cashBuff[1])*$flowerNum;
                }
                if ($cashBuff[0] == 1){
                    $goldNum = ceil($flowerReward['gold']*(1+$cashBuff[1]/100))*$flowerNum;
                }
            }
            //20200409
            //获取玩家该花朵是否具有经验加成
            $experienceBuff = FlowersUpgradeService::get()->getFlowerBuff($userId,$flower,GameConst::FLOWER_TYPE_2);
            if (!empty($experienceBuff)){
                if ($experienceBuff[0] == 2){
                    $experienceNum = ($flowerReward['experience'] + $experienceBuff[1])*$flowerNum;
                }
                if ($experienceBuff[0] == 1){
                    $experienceNum = ceil($flowerReward['experience']*(1+$experienceBuff[1]/100))*$flowerNum;
                }
            }

            $reward[GameConst::ENTITY_ACTION_GET_EXP] += intval($experienceNum);
            $reward[GameConst::ENTITY_ACTION_GET_TOKEN] += intval($goldNum);
        }

        //随出的鲜花挑战
        $orderInfo = SzPersister::get()->getVo($userId, GameConst::ORM_NAME_CHALLENGE_ORDER_INFO);
        if (!$orderInfo){
            $orderInfo = new ChallengeOrderInfoVo($userId,[],[],$reqTime,true);
        }

        $orderInfo->setTarget($targets);
        $orderInfo->setReward($reward);
        $orderInfo->setCreateTime($reqTime);
        SzPersister::get()->setVo($orderInfo);
        
        return array(
            'targets' => $targets,
            'reward' => $reward,
            'rewardMultiple' => $rewardMultiple,
        );
    }

}